
function Level2Comments(commentId) {
    $.ajax({
        url: "/discussionItem/retrieveComments",
        type: "POST",
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify({
            "type": 2,
            "targetId": commentId,
        }),
        success: function (response) {
            console.log(response);
            if (response.code === 200) {
                appendingLevel2Comments(commentId, response.data);
            }
        },
        error: function (error) {
            console.log(error);
        }
    });
}

function appendingLevel2Comments(commentId, comments) {
    let selector = "#level2Comments-" + commentId;
    $(selector).empty();
    for (let i = 0; i < comments.length; i++) {
        let a = "<div class=\"media\">\n" +
            "                                <div class=\"media-body\">\n" +
            "                                    <h4 class=\"media-heading\">\n" +
            "                                        <span>" + comments[i].userComment.nickname + "</span>\n" +
            "                                        <span>回复</span>\n" +
            "                                    </h4>\n" +
            "                                   <p>" + comments[i].content + "</p>\n" +
            "                                </div>\n" +
            "                            </div>";
        // debugger;
        $(selector).append(a);
    }

}

function clickToShow(commentId) {
    let selector = "#comment-" + commentId;
    let status = $(selector).css("display");
    if (status === "none") {
        $(selector).show();
    } else {
        $(selector).hide();
    }
    Level2Comments(commentId);
}

function replyComment(e) {
    let commentId = $(e).parent().attr("id").split("-").pop();
    let content = $(e).siblings("input").val();
    if (content.trim() === "") {
        alert("发送评论不能为空！！")
        return;
    }
    let userCommented = $(e).parent().attr("data-userComment").split("-").pop();
    reply(2, commentId, content,userCommented);
    alert("评论成功！！！");
    Level2Comments(commentId);
}

function comments() {
    let targetId = location.href.split("/").pop();
    $.ajax({
        url: "/discussion/retrieveComments/",
        type: "POST",
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify({
            "targetId": targetId,
            "type": 1//获取一级评论
        }),
        success: function (response) {
            appendingComments(response.data);
        },
        error: function (error) {
            console.log(error);
        }
    });
}

function appendingComments(comments) {
    let selector = ".replies";
    $(selector).empty();
    for (let i = 0; i < comments.length; i++) {
        let a = "<div class=\"media\">\n" +
            "                        <div class=\"media-left\">\n" +
            "                            <a href=\"#\">\n" +
            "                                <img class=\"media-object img-circle\" src=\"" + comments[i].userComment.avatarUrl + "\" alt=\"...\">\n" +
            "                            </a>\n" +
            "                        </div>\n" +
            "                        <div class=\"media-body\">\n" +
            "                            <h4 class=\"media-heading\">\n" +
            "                                <span>" + comments[i].userComment.nickname + "</span>\n" +
            "                                <span class=\"glyphicon glyphicon-time pull-right\">" + comments[i].modifiedAt + "</span>\n" +
            "                                <span class=\"glyphicon glyphicon-thumbs-up pull-right\">" + comments[i].likeCount + "</span>" +
            "                                <small>Lv<span>" + comments[i].userComment.level + "</span></small>\n" +
            "                            </h4>" + "<p>" + comments[i].content + "</p>" + "<a class=\"glyphicon glyphicon-comment\" href='javascript:clickToShow(" + comments[i].id + ")'></a>" +
            "                        </div>\n" +
            "                       <div class=\"form-group\" data-userComment='" + comments[i].userComment.id + "' id='comment-" + comments[i].id + "' style=\"display: none;\">\n" +
            "                            <input type=\"text\" class=\"form-control\">\n" +
            "                            <div class=\"form-control btn btn-success replyComment\" onclick='replyComment(this)'>回复</div>\n" +
            "                        </div>" +
            "<div id='level2Comments-" + comments[i].id + "'></div>" +
            "                    </div>";
        $(selector).append(a);
    }
}

function reply(type, targetId, content,userCommented) {
    $.ajax({
        url: "/comment",
        type: "POST",
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify({
            "type": type,
            "content": content,
            "targetId": targetId,
            "userCommented":userCommented
        }),
        success: function (response) {
            console.log(response);
            alert("评论成功！！");
            comments();
        },
        error: function (error) {
            console.log(error);
        }
    });
}

function appendingAuthor(author) {
    $(".DIUserId").attr("value",author.id);
    $(".DIAvatarUrl").attr("src", author.avatarUrl);
    $(".DINickname").text(author.nickname);
    $(".DILevel").text(author.level);
    $(".DIDescription").text(author.description);
}

function appendingDiscussion(discussion) {
    $(".discussionId").attr("value", discussion.id);
    $(".title").text(discussion.title);
    $(".content").text(discussion.content);
    $(".likeCount").text(discussion.likeCount);
    $(".readCount").text(discussion.readCount);
    $(".modifiedAt").text(discussion.modifiedAt);
    $(".classification").text(discussion.classification);
    $(function () {
        editormd.markdownToHTML("show-view", {});
    })

}
